Imports System.ComponentModel
Imports System.Text
Imports System.Security.Cryptography
Imports System.Threading
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports System.Net

Module General
    Private CheckConn As New CheckInternetConnection()

    Public Function MD5Hash (ByVal strToEncrypt As String) As String
        Dim ue As UTF8Encoding = New UTF8Encoding
        Dim bytes As Byte() = ue.GetBytes (strToEncrypt)
        Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
        Dim hashBytes As Byte() = md5.ComputeHash (bytes)
        Dim hashString As String = ""
        Dim i As Integer = 0
        While i < hashBytes.Length
            hashString += Convert.ToString (hashBytes (i), 16).PadLeft (2, "0"c)
            Math.Min (Interlocked.Increment (i), i - 1)
        End While
        Return hashString.PadLeft (32, "0"c)
    End Function

    Function RunWorkerAsyncIfNotBusy (ByVal BW As BackgroundWorker, Optional ByVal argument As Object = Nothing) _
        As Boolean
        If Not BW.IsBusy Then
            BW.RunWorkerAsync (argument)
            Return True
        Else
            Return False
        End If
    End Function

    Public Function URLEncode (ByVal s As String) As String
        If s IsNot Nothing Then
            'Dim encodedBytes As Byte() = (New Text.UTF8Encoding).GetBytes(s)
            Return Uri.EscapeUriString (s)
        Else
            Return ""
        End If
    End Function

    Public Sub SerializeToFile (Of T) (ByVal path As String, ByVal obj As T)
        Using fs As New FileStream (path, FileMode.Create)
            Dim bf As New BinaryFormatter (Nothing, _
                                           New StreamingContext (StreamingContextStates.File))
            bf.Serialize (fs, obj)
        End Using
    End Sub

    Public Function DeSerializeFromFile (Of T) (ByVal path As String) As T
        Using fs As New FileStream (path, FileMode.Open)
            Dim bf As New BinaryFormatter (Nothing, _
                                           New StreamingContext (StreamingContextStates.File))
            Return DirectCast (bf.Deserialize (fs), T)
        End Using
    End Function

    Public Function GetUnixTimeSpamp (ByVal Fecha As Date) As Long
        Dim FechaBase As New DateTime (1970, 1, 1, 0, 0, 0, 0)
        Dim span As TimeSpan = Fecha.ToUniversalTime() - FechaBase
        Return CLng (span.TotalSeconds)
    End Function

    Public Function isWindows7() As Boolean
        If My.Computer.Info.OSVersion < "6.1" Then
            Return False
        ElseIf My.Computer.Info.OSVersion >= "6.1" Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetAdvert (ByVal filePath As String) As Image
        If CheckConn.IsConnected = True Then
            Try
                Dim l_WebClient As New WebClient()
                Dim l_imageBytes As Byte() = l_WebClient.DownloadData (filePath)
                Dim l_stream As New MemoryStream (l_imageBytes)
                Return Image.FromStream (l_stream)
            Catch WebException As Exception
                Return Nothing
            End Try
        Else
            Return Nothing
        End If
    End Function

    Public Function GetAdvertUrl (ByVal filePath As String) As String
        If CheckConn.IsConnected = True Then
            Try
                Dim l_WebClient As New WebClient()
                Dim l_String As String = l_WebClient.DownloadString (filePath)
                Return l_String
            Catch WebException As Exception
                Return Nothing
            End Try
        Else
            Return Nothing
        End If
    End Function

    Public Function GetAppVersion (Optional ByVal format As String = _
                                      "v[M].[m].[b]") As String
        With My.Application.Info
            GetAppVersion = format.Replace ("[M]", .Version.Major.ToString)
            GetAppVersion = GetAppVersion.Replace ("[m]", .Version.Minor.ToString)
            GetAppVersion = GetAppVersion.Replace ("[b]", .Version.Build.ToString)
            GetAppVersion = GetAppVersion.Replace ("[r]", .Version.Revision.ToString)
        End With
    End Function
End Module
